<?php
namespace App\Admin\Logic\Dhe;

use App\Admin\Logic\BaseLogic;
use App\Logic\MachineLogic;
use App\Models\SwtDheMachine;
use App\Services\DaHuangAdminService;

class DheMachineLogic extends BaseLogic
{
    /**
     * @notes 获取大黄鹅设备列表
     * @param $machine_code
     * @param $arr
     * @return array|mixed
     * @throws \Illuminate\Http\Client\RequestException
     * @author Evil丿
     * @date 2024/06/20 10:56
     */
    public static function getMachineList($machine_code = "", $arr = []) {
        $list = DaHuangAdminService::getMachineList($machine_code);

        $arr = array_merge($arr, $list);

        if(count($list) >= 1000) {
            $machine_info = end($list);
            return self::getMachineList($machine_info['machineCode'], $arr);
        }

        return $arr;
    }

    /**
     * @notes 刷新本地大黄鹅设备列表
     * @return string
     * @throws \Illuminate\Http\Client\RequestException
     * @author Evil丿
     * @date 2024/06/20 10:56
     */
    public static function refreshMachineList() {
        $list = self::getMachineList();

        $update_num = 0;
        $insert_num = 0;

        foreach ($list as $item) {
            if(substr($item['machineCode'], 0, 3) == '518') {
                continue;
            }

            $info = SwtDheMachine::where('machine_code', $item['machineCode'])->first();

            if($info) {
                $data = [
                    'sale_status'       => $item['status'],
                    'pay_type'          => trim($item['payType'], ','),
                    'machine_name'      => $item['machineName'] ?? '大黄鹅：'.$item['machineCode'],
                ];
                $res = SwtDheMachine::where('machine_code', $item['machineCode']) -> update($data);

                if($res) {
                    $update_num ++;
                }
            } else {
                $data = [
                    'machine_code' => $item['machineCode'],
                    'machine_name' => $item['machineName'] ?? '大黄鹅：' . $item['machineCode'],
                    'pay_type'     => trim($item['payType'], ','),
                    'sale_status'  => $item['status'],
                    'created_at'   => $item['gmtCreated']
                ];
                $res = SwtDheMachine::create($data);
                if($res) {
                    $insert_num ++;
                }
            }

        }

        return '新增'.$insert_num.'条, 修改'.$update_num.'条，共计'.count($list).'条数据';
    }

    /**
     * @notes 刷新大黄鹅设备信息
     * @param $machine_code
     * @return bool
     * @throws \Illuminate\Http\Client\RequestException
     * @author Evil丿
     * @date 2024/07/29 16:35
     */
    public static function refreshMachineInfo($machine_code) {
        $info = DaHuangAdminService::getMachineInfo($machine_code);

        $data = [
            'online_status'    => $info['online'] ? 1 : 0,
            'last_online_time' => $info['onlineTime'] ?? null,
            'driver_version'   => $info['driverVersion'] ?? null,
            'android_version'  => $info['androidVersion'] ?? null,
            'door_status'      => $info['doorStatus'] ? 1 : 0,
            'door_action_at'   => $info['doorDate'] ?? null,
            'power_status'     => $info['powerStatus'],
            'temperature'      => $info['temperature'] ?? '',
            'longitude'        => $info['longitude'] ?? null,
            'latitude'         => $info['latitude'] ?? null,
            'address'          => $info['address'] ?? null
        ];

        $res = SwtDheMachine::where('machine_code', $machine_code) -> update($data);

        if(!$res) {
            self::$error = "数据更新失败";
            return false;
        }

        return true;
    }

    /**
     * @notes 获取大黄鹅设备商品
     * @param $machine_code
     * @return mixed
     * @author Evil丿
     * @date 2024/08/05 09:17
     */
    public static function getDHEMachineGoods($machine_code)
    {
        $data = DaHuangAdminService::getMachineGoods($machine_code);

        if($data) {
            foreach($data as &$item)
            {
                $item['goodsPrice'] /= 100;
                $item['originalPrice'] /= 100;
            }
        }

        return $data;
    }

    public static function asyncMachineToWarehouse($ids) {
        $machines = SwtDheMachine::whereIn('id', $ids)->get();

        if(empty($machines)) {
            self::$error = "设备不存在";
            return false;
        }

        $num = 0;

        foreach($machines as $machine) {
            $data = [
                'machine_no'        => $machine['machine_code'],
                'machine_name'      => $machine['machine_name'],
                'address'           => $machine['address'],
                'source_type'       => 1
            ];


            $res = MachineLogic::addMachine($data);

            if($res) {
                SwtDheMachine::where('id', $machine['id']) -> update(['machine_id' => $res]);
                $num ++;
            }
        }

        return $num;
    }
}
